package org.tlipoca.smarticlelike.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.genntii.smcommon.constant.GatewayConstant;
import org.genntii.smcommon.result.Result;
import org.genntii.smdomain.dto.ArticleLikeDTO;
import org.genntii.smdomain.vo.LikeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tlipoca.smarticlelike.service.ArticleLikeService;

@Tag(name = "文章点赞接口")
@Slf4j
@RestController
@RequestMapping("/article_like")
public class ArticleLikeController {

    @Autowired
    private ArticleLikeService articleLikeService;

    @Operation(summary = "文章点赞情况")
    @GetMapping
    public Result<LikeVO> getArticleLike(@RequestParam("articleId") Long articleId, @RequestParam("userId") Long userId ) {
        return Result.success(articleLikeService.getArticleLike(articleId, userId));
    }

    @Operation(summary = "点赞点踩文章")
    @PostMapping
    public Result likeArticle(@RequestBody ArticleLikeDTO articleLikeDTO,  HttpServletRequest request) {
        Long userId = Long.valueOf(request.getHeader(GatewayConstant.USER_ID));
        articleLikeService.articleLike(articleLikeDTO.getArticleId(),articleLikeDTO.getLike(),userId);
        return Result.success();
    }

    @Operation(summary = "取消点赞")
    @DeleteMapping
    public Result delLike(HttpServletRequest request, @RequestParam("articleId")Long articleId){
        Long userId = Long.valueOf(request.getHeader(GatewayConstant.USER_ID));
        articleLikeService.delArticleLike(articleId,userId);
        return Result.success();
    }

}
